package com.ibeeking.found.upms.b.rest.service.impl;

import com.ibeeking.found.upms.b.rest.service.IShopPermissionService;
import com.ibeeking.found.common.enums.FrontendTypeEnum;
import com.ibeeking.found.upms.api.common.dto.PermissionDTO;
import com.ibeeking.found.upms.api.common.param.PermissionParam;
import com.ibeeking.found.upms.api.feign.UpmsGClient;
import com.ibeeking.found.upms.service.common.bo.ShopPermissionBO;
import com.ibeeking.found.upms.service.common.dos.ShopPermissionDO;
import com.ibeeking.found.upms.service.common.vo.PermissionTreeVO;
import com.ibeeking.found.upms.service.mapper.ShopPermissionMapper;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.TreeFastUtils;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName ShopPermissionServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-05-11 16:57
 **/
@Service
public class ShopPermissionServiceImpl implements IShopPermissionService {

    @Resource
    private ShopPermissionMapper shopPermissionMapper;
    @Resource
    private UpmsGClient upmsGClient;

    @Override
    public List<ShopPermissionBO> queryEffective() {
        List<ShopPermissionDO> shopPermissionDos = shopPermissionMapper.selectList(Wrappers.emptyWrapper());
        if (Collections3.isEmpty(shopPermissionDos)) {
            return Collections3.emptyList();
        }
        ResponseResult<List<PermissionDTO>> responseResult = upmsGClient.queryAllList(Arrays.asList(FrontendTypeEnum.SHOP.getCode()));
        if (null == responseResult || Collections3.isEmpty(responseResult.getData())) {
            return Collections3.emptyList();
        }
        List<PermissionTreeVO> permissionTreeVos = JsonUtils.jsonToList(responseResult.getData(), PermissionTreeVO.class);
        responseResult = null;
        List<Long> haveIds = shopPermissionDos.stream().map(ShopPermissionDO::getId).collect(Collectors.toList());
        List<Long> resultIds = new ArrayList<>();
        TreeFastUtils.selectLongPidByCid(resultIds, permissionTreeVos, haveIds);
        resultIds.addAll(haveIds);
        List<PermissionTreeVO> collect = permissionTreeVos.stream().filter(s -> resultIds.contains(s.getId())).collect(Collectors.toList());
        return JsonUtils.jsonToList(collect, ShopPermissionBO.class);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Boolean configurePermission(List<PermissionParam> params) {
        shopPermissionMapper.delete(Wrappers.emptyWrapper());
        List<ShopPermissionDO> shopPermissionDos = JsonUtils.jsonToList(params, ShopPermissionDO.class);
        shopPermissionDos.stream().forEach(d->{
           d.setCreateTime(LocalDateTime.now());
        });
        return shopPermissionMapper.insertBatchSomeColumn(shopPermissionDos) > 0;
    }
}
